/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 *
 * @author Administrator
 */
public class SqlServerDbTool {
       public Connection con = null;
       
       private String host;
       private String user;
       private String password;
       private String database;

    public SqlServerDbTool(String host, String user, String password,String database) {
        this.host = host;
        this.user = user;
        this.password = password;
        this.database = database;
    }
       
     
      public void sqlServerConnect() {
        try {
            Properties pr;
            pr = new Properties();
//            pr.put("characterEncoding", "UTF-8");
            pr.put("useUnicode", "TRUE");
            pr.put("user", user);
            pr.put("password", password);
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            con = DriverManager.getConnection("jdbc:sqlserver://"+host+"; DatabaseName="+database, pr); //dcmris_en
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
        public void sqlServerDisconnect() {
        try {
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public ResultSet sqlServerGetQ(String q) {
        ResultSet RS = null;
        try {
            Statement Stmt = con.createStatement();
            RS = Stmt.executeQuery(q);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return RS;
    }

    public int sqlServerSetQ(String q) {
        try {
            Statement stmt = con.createStatement();
            stmt.executeUpdate(q);
            ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException ex) {
        
            ex.printStackTrace();
        }
        return -1;
    }
}
